<?php
namespace Home\Model;

use Think\Model;
use \Webchat\THWebchat;

class Webchat extends Model
{
	public $webchat = null;
	public $id = null;
	public $user_id = null;

	public function __construct()
	{
		$options = array(
	        
	        'token' => Setting::getOption('token'),
	        'encodingaeskey' => Setting::getOption('encodingaeskey'),
	        'appid' => Setting::getOption('appid'),
	        'appsecret' => Setting::getOption('appsecret'),
        );

    	$this->webchat = new THWebchat($options);
    	
	}

	public function process() {
		$this->webchat->valid();
		$type = $this->webchat->getRev()->getRevType();
		$this->id = $this->webchat->getRevFrom();

		switch($type) {
			case THWebchat::MSGTYPE_TEXT:
				$this->handleMsg();              
				break;
			case THWebchat::MSGTYPE_EVENT:
				$this->handleEvent();
				break;
			case THWebchat::MSGTYPE_IMAGE:
				//break;
			default:
				$this->webchat->text("不支持")->reply();
		}
	}
	private function handleMsg($msg = null) {
		if ($msg == null) {
			$msg = $this->webchat->getRevContent();
		}

		$cmd = mb_substr($msg, 0, 2, 'utf-8');

		switch ($cmd) {
			case '你好':
				$sendMsg = '你好。';
				break;
			case '状态':
				$sendMsg = '你好。';
				break;
			case '开灯':
				$sendMsg = '测试';
				break;
			case 'bd':
				// "bd kasjdf asdf"
				$data = explode(" ", $msg);
				if (count($data) == 3) {
					$userModel = D('User');
					$user = $userModel->where(['username'=>$data[1]])->find();
					if ($user && $user['password'] == $data[2]) {
						$userModel->webchat_id = $this->id;
						$userModel->save();
						$sendMsg = '绑定成功！';
					}
					else {
						$sendMsg = '用户名或密码错误！';
					}
				}
				else {
					$sendMsg = '指令格式错误！';
				}
				break;
			default:
				$sendMsg = '??';
				break;
		}
        
        $this->webchat->text($sendMsg)->reply();
	}

	private function handleEvent() {
		$event = $this->webchat->getRevEvent();
		$sendMsg = null;

		switch ($event['key']) {
			case 'status':
				$sendMsg = $this->generalMsg('status');
				break;
			case 'open_light':
				$sendMsg = $this->generalMsg('light');
				break;
			case 'close_light':
				$sendMsg = $this->generalMsg('dlight');
				break;
			case 'wf':
				$sendMsg = $this->generalMsg('wf');
				break;
			default:
				$sendMsg = '未设置事件';
				break;
		}

		$this->webchat->text($sendMsg)->reply();
	}

	private function generalMsg($type) {
		
		switch ($type) {
			case 'status':
				$this->bdNeeded();
				$model = D('UserData');
				$data = $model->where(['user_id' => $this->user_id])->find();

				$msg = '现在室内温度'.$data['temperature'].'°C, 湿度为'.$data['humitity'].'%';
				break;
			case 'light':
				$this->bdNeeded();
				$this->lightSwitch(true);
				$msg = '已设置开灯。';
				break;
			case 'dlight':
				$this->bdNeeded();
				$this->lightSwitch(false);
				$msg = '已设置关灯。';
				break;
			default:
				$msg = '未设置事件';
				break;
		}

		return $msg;
	}

	private function bdNeeded() {
		$userModel = D('User');
		$user = $userModel->where(['webchat_id'=>$this->id])->find();
		if (!$user) {
			$this->webchat->text(<<<TEXT
				请先绑定账号。回复"bd 账号 密码"进行绑定。
TEXT
			)->reply();
			exit(0);
		}

		$this->user_id = $user['id'];
	}

	private function lightSwitch($status) {
		$model = D('UserData');
		$data = $model->where(['user_id'=>$this->user_id])->find();
		$con = $data['control'];

		$control = [];
		if ($con) {
			$control = json_decode($con, true);
		}

		$control['light'] = $status;
		$model->where(['id'=>$data['id']])
		->save(['control'=>json_encode($control)]);
	}
}
